/**
 * author:Administrator
 * date:2013-5-21
 */
package com.roc.web.action.terminal;

import java.util.ArrayList;
import java.util.List;

import com.roc.page.BaseSearchCondition;
import com.roc.page.WhereHQLFieldBean;
import com.roc.util.Constance;
import com.roc.util.constant.Role;

/**
 * 
 * @author 陈锴
 * 
 * 终端序列号添加
 * 
 * 2014-08-18
 *
 */
public class TerminalListSearchCondition extends BaseSearchCondition {
	

	private String orgCode;
	private String merchantNo;
	private String merchantName;
	private String tid;
	private String flag;
	private String terminalSn;	
	private long agentId;
    private String opId;
    private String roleType;
    private String oneOrgCode;//机构号
    private String groupCd;
    private String direct;
	/* (non-Javadoc)
	 * @see com.roc.page.BaseSearchCondition#getOrderPartOfHQL()
	 */
	@Override
	protected String getOrderPartOfHQL() {
		// TODO Auto-generated method stub
		
		return " ORDER BY F.MCHT_CD, E.TERM_ID DESC ";
	}

	/* (non-Javadoc)
	 * @see com.roc.page.BaseSearchCondition#getRowCountHQL()
	 */
	@Override
	protected String getRowCountHQL() {//,OrgBean o  t.orgCode = o.org_code
		StringBuffer sb=new StringBuffer();
		sb.append(" SELECT COUNT(*) ");
		sb.append(" FROM TBL_TERM_INF E,TBL_MCHT_INF F");
		sb.append(" WHERE E.MCHT_CD = F.MCHT_CD ");
		if(roleType.equals(Role.MARKET_DIRECT.getCode()) ||roleType.equals(Role.MARKET_LEAD.getCode()))
		{
			sb.append("  AND EXISTS (SELECT 1  FROM T_OPER_ROLE OROLE ");
	        sb.append("    WHERE F.UC_BC_CD_AREA = OROLE.AGENTID ");
	        sb.append("      AND OROLE.OPERID =").append(opId).append(")");
		}
		if(groupCd.equals("2")){
			sb.append(" AND F.GROUP_CD='2' ");
		}
		if(roleType.equals(Role.O_MANAGER.getCode())){
			if(direct!=null){
				sb.append("  AND EXISTS (SELECT 1 FROM (SELECT P.ID FROM T_OPERATORS P ");
				sb.append("  WHERE P.SUPRI_ID=").append(opId);
				sb.append("  AND P.ROLE_TYPE='03' ");
				sb.append("  UNION ALL SELECT P.ID FROM T_OPERATORS P");
				sb.append("  WHERE P.ID=").append(opId).append(" AND P.ROLE_TYPE='02') S");
				sb.append("  WHERE S.ID=F.EXT1)");
			}else{
				sb.append(" AND F.EXT1=").append(opId);
			}
		}
		return sb.toString();
	}

	/* (non-Javadoc)
	 * @see com.roc.page.BaseSearchCondition#getSearchHQL()
	 */
	@Override
	protected String getSearchHQL() {
		StringBuffer sb=new StringBuffer();
		sb.append(" SELECT F.BANK_BELONG_CD,F.MCHT_CD,F.NAME_BUSI,F.NAME,E.TERM_ID,E.TERM_TP,E.STATUS,E.DEVICE_CD,F.EXT1,F.REC_UPD_OPR ");
		sb.append(" FROM TBL_TERM_INF E,TBL_MCHT_INF F");
		sb.append(" WHERE E.MCHT_CD = F.MCHT_CD ");
		if(roleType.equals(Role.MARKET_DIRECT.getCode()) ||roleType.equals(Role.MARKET_LEAD.getCode()))
		{
			sb.append("  AND EXISTS (SELECT 1  FROM T_OPER_ROLE OROLE ");
	        sb.append("    WHERE F.UC_BC_CD_AREA = OROLE.AGENTID ");
	        sb.append("      AND OROLE.OPERID =").append(opId).append(")");
		}
		if(groupCd.equals("2")){
			sb.append(" AND F.GROUP_CD='2' ");
		}
		if(roleType.equals(Role.O_MANAGER.getCode())){
			if(direct!=null){
				sb.append("  AND EXISTS (SELECT 1 FROM (SELECT P.ID FROM T_OPERATORS P ");
				sb.append("  WHERE P.SUPRI_ID=").append(opId);
				sb.append("  AND P.ROLE_TYPE='03' ");
				sb.append("  UNION ALL SELECT P.ID FROM T_OPERATORS P");
				sb.append("  WHERE P.ID=").append(opId).append(" AND P.ROLE_TYPE='02') S");
				sb.append("  WHERE S.ID=F.EXT1)");
			}else{
				sb.append(" AND F.EXT1=").append(opId);
			}
		}
		return sb.toString();
	}

	/* (non-Javadoc)
	 * @see com.roc.page.BaseSearchCondition#getWhereHQLFieldBeanList()
	 */
	@Override
	protected List<WhereHQLFieldBean> getWhereHQLFieldBeanList() {
		List<WhereHQLFieldBean> filedBeanList = new ArrayList<WhereHQLFieldBean>();

		
		if (Constance.isNotEmpty(orgCode)) {
		
			filedBeanList.add(new WhereHQLFieldBean("F.BANK_BELONG_CD",orgCode, "=", false));
		}
		if(Constance.isNotEmpty(oneOrgCode)&&!(roleType.equals(Role.SYSTEM.getCode())||roleType.equals(Role.AGENT.getCode())||roleType.equals(Role.MARKET_DIRECT.getCode())||roleType.equals(Role.MARKET_LEAD.getCode()))){//小机构
    		filedBeanList.add(new WhereHQLFieldBean("F.UC_BC_CD_AREA", oneOrgCode.trim(), "=", false));
		}
		if (Constance.isNotEmpty(merchantName)) {
			filedBeanList.add(new WhereHQLFieldBean("F.NAME_BUSI", merchantName.trim(),
					"LIKE", false, "ALL"));
		}
		if (Constance.isNotEmpty(merchantNo)) {
			filedBeanList.add(new WhereHQLFieldBean("F.MCHT_CD", merchantNo.trim(), "=", false));
		}
		if (Constance.isNotEmpty(tid)) {
			filedBeanList.add(new WhereHQLFieldBean("E.TERM_ID", tid, "=", false));
		}
		if (Constance.isNotEmpty(flag)) {
			filedBeanList.add(new WhereHQLFieldBean("E.STATUS", flag, "=", false));
		}
		if (Constance.isNotEmpty(terminalSn)) {
			filedBeanList.add(new WhereHQLFieldBean("E.DEVICE_CD", terminalSn.trim(), "=", false));
		}
		if(roleType.equals(Role.AGENT.getCode()) || roleType.equals(Role.SFB_AGENT.getCode())){
			filedBeanList.add(new WhereHQLFieldBean("F.EXT1",opId.trim(),"=",false));
		}
		if(roleType.equals(Role.AGENT_BUSS.getCode())){//操作员 
	    	filedBeanList.add(new WhereHQLFieldBean("F.REC_UPD_OPR", opId.trim(), "=", false));
	    }
		return filedBeanList;
	}


	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getMerchantNo() {
		return merchantNo;
	}

	public void setMerchantNo(String merchantNo) {
		this.merchantNo = merchantNo;
	}

	public String getMerchantName() {
		return merchantName;
	}

	public void setMerchantName(String merchantName) {
		this.merchantName = merchantName;
	}

	public String getTid() {
		return tid;
	}

	public void setTid(String tid) {
		this.tid = tid;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getTerminalSn() {
		return terminalSn;
	}

	public void setTerminalSn(String terminalSn) {
		this.terminalSn = terminalSn;
	}

	public long getAgentId() {
		return agentId;
	}

	public void setAgentId(long agentId) {
		this.agentId = agentId;
	}

	public String getOpId() {
		return opId;
	}

	public void setOpId(String opId) {
		this.opId = opId;
	}

	public String getRoleType() {
		return roleType;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}

	public String getOneOrgCode() {
		return oneOrgCode;
	}

	public void setOneOrgCode(String oneOrgCode) {
		this.oneOrgCode = oneOrgCode;
	}

	public String getGroupCd() {
		return groupCd;
	}

	public void setGroupCd(String groupCd) {
		this.groupCd = groupCd;
	}

	public String getDirect() {
		return direct;
	}

	public void setDirect(String direct) {
		this.direct = direct;
	}
	
}
